/**
 *@Copyright:Copyright (c) 2014 - 2100
 *@Company:优问
 */
package com.auto.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.auto.base.mapper.SysModuleMapper;
import com.auto.base.mapper.SysParameterMapper;
import com.auto.base.pojo.SysModule;
import com.auto.base.pojo.SysModuleExample;
import com.auto.base.pojo.SysParameter;
import com.auto.base.pojo.SysParameterExample;
import com.auto.base.pojo.SysParameterExample.Criteria;
import com.base.common.CommonConstants;
import com.base.util.JUtil;
import com.base.util.SpringBeanUtil;


/**
 * @Title:
 * @Description:
 * @Author:yanghao
 * @Since:2014-12-15
 * @Version:1.0
 */
public class SystemUtil {
	
	private static SystemUtil systemHelper = null;
	
	private static Map<String, Object> INPUT_ERR_MAP = null;
	
	private static Map<String,String> paramMap = null;
	
	private static Set<String> allowUrl = null;
	
	private static String[] EXTRA_URI =new String[]{"/","index","jspmanagemain.jsp","jspmanagetop.jsp","jspmanageleft.jsp"};
	
	//private static Set<String> allowAction = null;
	
	private SystemUtil() {
	}
	
	
	private static class InitParamUtil{
		
		private static SysParameterMapper t = null;
		private static SysModuleMapper m = null;
		static {
			try {
				t  = SpringBeanUtil.getBean(SysParameterMapper.class);
				m = SpringBeanUtil.getBean(SysModuleMapper.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
			loadParam();
			loadModule();
		}
		
		public static void loadParam(){
			try{
				paramMap = new HashMap<String, String>(); 
				SysParameterExample example = new SysParameterExample();
				Criteria criteria = example.createCriteria();
				criteria.andEnabledEqualTo(CommonConstants.ENABLE);
				List<SysParameter> paramsList = t.selectByExample(example);
				if(null != paramsList && !paramsList.isEmpty()){
					for(SysParameter c : paramsList){
						String mapKey = c.getParameterKey();
						paramMap.put(mapKey, c.getParameterValue());
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		public static void loadModule(){
			try{
				if(null ==allowUrl ){
					allowUrl = new HashSet<String>(); 
				}
				SysModuleExample example = new SysModuleExample();
				SysModuleExample.Criteria  criteria = example.createCriteria();
				criteria.andEnabledEqualTo(CommonConstants.ENABLE);
				List<SysModule> paramsList = m.selectByExample(example);
				if(null != paramsList && !paramsList.isEmpty()){
					for(SysModule c : paramsList){
						if(!JUtil.isNull(c.getModuleUrl()) && !"/".equals(c.getModuleUrl())){
							String url  = c.getModuleUrl();
							 url = url.replace("/", "");
							allowUrl.add(url);
						}
					}
				}
			}catch(Exception e){
				
			}
		}
		
		
		
	}
	
	private static class InitSystemHelper {
		private final static SystemUtil instance = new SystemUtil();
	}
	
	public boolean isContainExtraPath(String url){
		for(String s:EXTRA_URI){
			if(s.equals(url)){
				return true;
			}
		}
		return false;
	}
	
	public  Map<String,String> getParamMap(){
		return paramMap;
	}
	
	public  String getParamMapValue(String key){
		if(null == paramMap){
			return null;
		}
		return paramMap.get(key);
	}
	
	public  void setParamMapValue(String key,String value){
		if(null == paramMap){
			return ;
		}
		paramMap.put(key, value);
	}
	
	public  void removeParamMapByKey(String key){
		if(null == paramMap){
			return ;
		}
		paramMap.remove(key);
	}
	
	public Set<String> getAllowUrl(){
		return allowUrl;
	}
	
	public void setAllowUrl(Set<String> allowUrl_){
		allowUrl = allowUrl_;
	}
	
	public boolean isContainsUrl(String url){
		if(null == allowUrl || allowUrl.isEmpty()){
			return false;
		}
//		for(String url_ : allowUrl){
//			if(url.contains(url_)){
//				return true;
//			}
//		}
		return allowUrl.contains(url);
	}
	
	public void addAllowUrl(String url){
		 allowUrl.add(url);
	}
	
	public void removeAllowUrl(String url){
		 allowUrl.remove(url);
	}
	
	public  void setParamMap(Map<String,String> paramMap_){
		paramMap = paramMap_;
	}
	
	public void toReMoveErrMap(String key) {
		if (null == key) {
			return;
		}
		if (null == INPUT_ERR_MAP) {
			return;
		}
		INPUT_ERR_MAP.remove(key);
	}
	
	public Object getErrMap(String key) {
		if (null == key) {
			return null;
		}
		if (null == INPUT_ERR_MAP) {
			return null;
		}
		return INPUT_ERR_MAP.get(key);
	}
	
	public void setErrMap(String key,Object value) {
		if(null == key) {
			return;
		}
		if(null == INPUT_ERR_MAP) {
			INPUT_ERR_MAP = new HashMap<String, Object>();
		}
		INPUT_ERR_MAP.put(key,value);
	}
	
	public static SystemUtil getInstance() {
		
		if(null == paramMap){
			InitParamUtil.loadParam();
		}
		if(null == allowUrl){
			InitParamUtil.loadModule();
		}
		if (null == systemHelper) {
			return InitSystemHelper.instance;
		}
		
		return systemHelper;
	}
	
}
